package app.goods.info.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.channels.FileChannel;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import app.goods.info.constants.ConstontValue;

public class CommonUtils {
	/**
	 * 首字母大写
	 * 
	 * @param fieldName
	 * @return
	 */
	public static String getFirstUpcase(String fieldName) {
		String str = "";
		str = fieldName;
		if (!StringUtils.isEmpty(str)) {
			str = str.substring(0, 1).toUpperCase() + str.substring(1);
		}
		return str;
	}

	/**
	 * 首字母小写
	 * 
	 * @param fieldName
	 * @return
	 */
	public static String getFirstLowcase(String fieldName) {
		String str = "";
		str = fieldName;
		if (!StringUtils.isEmpty(str)) {
			str = str.substring(0, 1).toLowerCase() + str.substring(1);
		}
		return str;
	}

	/**
	 * 以行为单位读取文件，常用于读面向行的格式化文件
	 */
	public static StringBuilder readFileByLines(String fileName) {
		StringBuilder stringBuilder = new StringBuilder();
		File file = new File(fileName);
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				stringBuilder.append(tempString);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
		return stringBuilder;
	}

	/**
	 * 以行为单位读取文件，常用于读面向行的格式化文件
	 */
	public static List<String> readFileByLines(File file) {
		List<String> contents = new ArrayList<String>();
		// StringBuilder contents = new StringBuilder();
		if (file != null && file.exists()) {
			BufferedReader reader = null;
			try {
				reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
				String tempString = null;
				// 一次读入一行，直到读入null为文件结束
				while ((tempString = reader.readLine()) != null) {
					contents.add(tempString);
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (reader != null) {
					try {
						reader.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		return contents;
	}

	/**
	 * 根据简单规则，将数据库里的字段名字转换为Java名字
	 * 
	 * @param columnName
	 * @return
	 */
	public static String getJavaFiledNameByColumnName(String columnName) {
		// String javaFiledName = columnName == null ? "" :
		// columnName.toLowerCase();
		String javaFiledName = columnName.toLowerCase();
		// 做个基本转换，如果含有_则转换一下
		// column_tset_tt---->columnTestTt
		if (StringUtils.isNotEmpty(javaFiledName)) {
			String[] tmpArray = javaFiledName.split("_");
			if (tmpArray.length > 0) {
				javaFiledName = tmpArray[0];
				for (int i = 1; i < tmpArray.length; i++) {
					javaFiledName = javaFiledName + CommonUtils.getFirstUpcase(tmpArray[i]);
				}
			}
		}
		return javaFiledName;
	}

	/**
	 * 使用文件通道的方式复制文件
	 * 
	 * @param srcFile  源文件
	 * @param destFile 复制到的新文件
	 */
	public static void fileChannelCopy(File srcFile, File destFile) {
		FileInputStream fi = null;
		FileOutputStream fo = null;
		FileChannel in = null;
		FileChannel out = null;
		try {
			fi = new FileInputStream(srcFile);
			fo = new FileOutputStream(destFile);
			in = fi.getChannel();// 得到对应的文件通道
			out = fo.getChannel();// 得到对应的文件通道
			in.transferTo(0, in.size(), out);// 连接两个通道，并且从in通道读取，然后写入out通道
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fi.close();
				in.close();
				fo.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static boolean isExpired() {
		boolean res = false;
		Date now = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		String expireDate = PropertiesUtils.getValueByKeyFromResources(ConstontValue.CONFIG_FILE_NAME, "expiresDate");
		// 解密
		expireDate = PropertiesUtils.getFromBase64(expireDate);
		try {
			if (now.after(sf.parse(expireDate))) {
				res = true;
			} else {
				res = false;
			}
		} catch (ParseException e) {
			res = true;
		}
		return res;
	}

	/**
	 * 递归删除目录下的所有文件及子目录下所有文件
	 * 
	 * @param dir 将要删除的文件目录
	 * @return boolean Returns "true" if all deletions were successful. If a
	 *         deletion fails, the method stops attempting to delete and returns
	 *         "false".
	 */
	public static boolean deleteDir(File dir) {
		if (dir.isDirectory()) {
			String[] children = dir.list();
			// 递归删除目录中的子目录下
			for (int i = 0; i < children.length; i++) {
				boolean success = deleteDir(new File(dir, children[i]));
				if (!success) {
					return false;
				}
			}
		}
		// 目录此时为空，可以删除
		return dir.delete();
	}
}
